package com.aaa.inbound.controller;


import com.aaa.entity.inbound.PurchaseDetail;
import com.aaa.inbound.service.PurchaseDetailService;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aaa
 * @since 2020-11-25
 */
@RestController
@RequestMapping("/inbound/purchase-detail")
@Api(description = "采购单详情管理")
public class PurchaseDetailController {

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @PostMapping("/searchPurchaseDetail/{pid}")
    @ApiOperation(value = "查询方法")
    public R searchPurchaseDetail(@ApiParam(value = "采购单id") @PathVariable Long pid){
        QueryWrapper<PurchaseDetail> wrapper = new QueryWrapper<>();
        wrapper.like("pid", pid);
        List<PurchaseDetail> list = purchaseDetailService.list(wrapper);
        return R.ok().code(200).message("查询成功").data("list",list);
    }

    @PostMapping("/addOrUpdPurchaseDetail")
    @ApiOperation(value = "新增/修改方法")
    public R addPurchase(@RequestBody PurchaseDetail purchaseDetail){
        QueryWrapper<PurchaseDetail> queryWrapper = new QueryWrapper();
        queryWrapper.eq("pid",purchaseDetail.getPid());
        queryWrapper.eq("commodityid",purchaseDetail.getCommodityid());
        PurchaseDetail one = purchaseDetailService.getOne(queryWrapper);
        if (one!=null){
            one.setQty(purchaseDetail.getQty()+one.getQty());
            boolean b = purchaseDetailService.updateById(one);
            if (b) {
                return R.ok().message("操作成功").code(200);
            }else {
                return R.error().message("操作失败").code(500);
            }
        }else{
            boolean b = purchaseDetailService.save(purchaseDetail);
            if (b) {
                return R.ok().message("操作成功").code(200);
            }else {
                return R.error().message("操作失败").code(500);
            }
        }



    }

    @DeleteMapping("/delPurchaseDetail/{id}")
    @ApiOperation(value = "根据ID删除")
    public R delPurchaseDetail(@PathVariable Long id){
        boolean b = purchaseDetailService.removeById(id);
        if (b) {
            return R.ok().message("删除成功").code(200);
        }else {
            return R.error().message("删除失败").code(500);
        }
    }

    @PostMapping("/findCommodity/{warehouseid}")
    @ApiOperation(value = "商品下拉查询")
    public List<Map> findCommodity(@ApiParam(value = "仓库ID") @PathVariable Long warehouseid){
        return purchaseDetailService.findCommodity(warehouseid);
    }

    @PostMapping("/addQty/{pid}")
    @ApiOperation(value = "求商品总数量")
    public int addQty(@ApiParam(value = "采购单id") @PathVariable Long pid){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("pid",pid);
        List<PurchaseDetail> list = purchaseDetailService.list(queryWrapper);
        int num=0;
        for (PurchaseDetail pd:list){
            num += pd.getQty();
        }
        return num;
    }
}

